var product;
var stockTable;
var server_path_shop;
var server_path = 'http://www.neo-flux.com';
$(document).ready(function(){
server_path_shop= $("#server_path_shop").val()

product = $('#product').val();
	
stockTable=$("#stocks_table").dataTable();

	$(".add_property").click(function(){
		$("#modal-attributes").modal();
		return false;
	});

	$(".add_property_value").click(function(){
		$("#modal-properties").modal();
		return false;
	});


	$("#add_attribute").click(function(){
		$('#attr_wrapper').append('<br class="clear"><hr />')
		$('#attr_wrapper').append('<div class="control-group ">'+$('#attr_wrapper div:first').html()+'</div>');
		
		$("#attr_wrapper .span6:last").html('');
		return false;
	});	

	$('.wysiwyg-editor').ace_wysiwyg({
		toolbar:
		[
		'font',
		null,
		'fontSize',
		null,
		{name:'bold', className:'btn-info'},
		{name:'italic', className:'btn-info'},
		{name:'strikethrough', className:'btn-info'},
		{name:'underline', className:'btn-info'},
		null,
		{name:'insertunorderedlist', className:'btn-success'},
		{name:'insertorderedlist', className:'btn-success'},
		{name:'outdent', className:'btn-purple'},
		{name:'indent', className:'btn-purple'},
		null,
		{name:'justifyleft', className:'btn-primary'},
		{name:'justifycenter', className:'btn-primary'},
		{name:'justifyright', className:'btn-primary'},
		{name:'justifyfull', className:'btn-inverse'},
		null,
		{name:'createLink', className:'btn-pink'},
		{name:'unlink', className:'btn-pink'},
		null,
		{name:'insertImage', className:'btn-success'},
		null,
		'foreColor',
		null,
		{name:'undo', className:'btn-grey'},
		{name:'redo', className:'btn-grey'}
		]
	}).prev().addClass('wysiwyg-style2');
	$(".chzn-select").chosen()
});
function show_attributes(value,select){
	sendData="get_properties="+value;
	$.ajax({
		type: "POST",
		url: server_path+"/wizard/libs/product_ajax.php",
		data: sendData,
		// dataType:'json',
		success: function(data){	
			$(select).parent().parent().children('.span6').html(data);

			$(".chzn-select").chosen()
		}
	});
}
function save_product(){
	description='';
	$(".wysiwyg-editor").each(function(key,value){
		description =description+ '&'+$(this).attr('id')+'='+$(this).html();
	});
	categories = $("#category").val();
	categs='';
	$.each(categories,function(key,value){
		categs+=value+',';
	});
	
	sendData="save_product=1&"+$("#product_descriptions").serialize()+description+'&type='+$('#type').val()+'&has_liqvidation='+$("#has_liqvidation").val()+'&visible='+$('#visible').val()+'&in_footer='+$('#in_footer').val()+'&category='+categs;
	if(product>0){
		sendData+='&product_id='+product;
	}
	attributes =new Array();
	$('.select_attribute').each(function(key,value){
		key = $(this).val();
		values= $(this).parent().parent().find('.span6 select').val();
		attributes[key]=values;
	});
	attr=JSON.stringify(attributes);
	sendData +='&attributes='+attr; 
	
	$.ajax({
		type: "POST",
		url: server_path+"/wizard/libs/product_ajax.php",
		data: sendData,
		// dataType:'json',
		success: function(data){	
			$("#stocks_wrapper").html(data);
			stockTable = $('#stocks_table').dataTable();
			$("#stocks_head_tab").trigger('click');
			alert(product);
		}
	});
	// alert(sendData);
	// $.ajax({
	// 	type: "POST",
	// 	url: "http://www.neo-flux.com/shop/crons/rss_feed.php",
	// 	data: sendData,
	// 	// dataType:'json',
	// 	success: function(){	
			
	// 	}
	// });

}
function update_stock(field,stock,obj){

	value = $(obj).val();
	valid=1;
	if((field=='quantity' || field=='price') && isNaN(value)){
		
		bootbox.alert('This value must be a number.');
		$(obj).val(0);
		$(obj).focus();
		valid=0;
	}		

	if(valid==1){
		sendData = "stock_field="+field+'&stock='+stock+'&value='+value;

		$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,
			// dataType:'json',
			success: function(data){	

			}
		});
	}
}
function update_stock_property(stock,obj){
	values=Array();	
	$(obj).closest('tr').find('.stock_properties').each(function(key,index){
		values[$(this).attr('id')]=$(this).val();
	});
	string = JSON.stringify(values);
	sendData = 'update_stock='+string+'&stock_id='+stock;
	$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,
			// dataType:'json',
			success: function(data){	

			}
		});
}
function add_stock(product1){
	if(typeof product1 === 'undefined'){
		product1 = product;
	}
	alert(product);
	alert(product1);
	sendData= 'add_stock='+product1;
	$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,
			dataType:'json',
			success: function(data){	
				
				var addId = stockTable.fnAddData( [data['0'],data['1'],data['2'],data['3'],data['4'],data['5'],data['6']]);
				stockTable.fnPageChange( 'last' );
			}
		});
}
function delete_stock(stock,obj){
	bootbox.confirm("Are you sure?", function(result) {
		sendData= 'remove_stock='+stock;
		$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,			
			success: function(data){			
				row = $(obj).closest("tr").get(0);
				stockTable.fnDeleteRow(stockTable.fnGetPosition(row));
			}
		});
	});
}
function delete_selected(){
	var vals=''
	$('.delete_selected').each(function(index,value){
		if($(this).is(":checked")){
			vals+=$(this).val()+',';
		}	
	});
	bootbox.confirm("Are you sure?", function(result) {
		sendData = 'delete_selected_stocks='+vals;
		$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,
		// dataType:'json',
		success: function(data){
			$('.delete_selected').each(function(index,value){
				if($(this).is(":checked")){
					row = $(this).closest("tr").get(0);
					stockTable.fnDeleteRow(stockTable.fnGetPosition(row));
				}
			});
		}
	})
	});
}
function save_attribute(){
	sendData = $('#properties_form').serialize()+'&add_attribute=1';
	$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,
		// dataType:'json',
		success: function(data){
			$('.select_attribute').each(function(key,value){
				sel=$(this).val();
				$(this).html(data);
				$(this).val(sel);
			});
		}
	});
}
function save_property(){
	sendData = $('#properties_values_form').serialize()+'&add_property=1'+'&pr_id='+product;
	$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,
			// dataType:'json',
			success: function(data){
				val = $('#att_select').val();
				$("#attr_wrapper select:has(option[value="+val+"]:selected)").parent().parent().find('.chzn-select').parent().html(data);
				$(".chzn-select").chosen();
			}
	});
}
function make_visible(id,o){
	if($(o).is(':checked')){
		val=1;
	}else{
		val=0;
	}
sendData = "make_comment_visible="+id+'&check='+val;
	$.ajax({
			type: "POST",
			url: server_path+"/wizard/libs/product_ajax.php",
			data: sendData,
			// dataType:'json',
			success: function(data){
				
			}
	});
}